package com.csnf.servlet;

import com.csnf.mapper.OrderMapper;
import com.csnf.util.Order;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getOrderByOrder")
public class GetOrderByOrderServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String orderCode = request.getParameter("orderCode");
        String orderState = request.getParameter("orderState");
        Order order = new Order();
        order.setOrderCode(orderCode);
        order.setOrderState(orderState);
        String resource = "mybatis-config.xml";
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
        OrderMapper mapper = sqlSessionFactory.openSession(true).getMapper(OrderMapper.class);
        List<Order> orders = mapper.getOrderByOrder(order);
        request.setAttribute("Orders",orders);
        request.setAttribute("o",order);
        request.getRequestDispatcher("OrderList.jsp").forward(request,response);
    }
}
